// pages/reviewReport/reviewReport.js
import {
  request2
} from "../../utils/request2";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isNull: true,
    taskList:[],
    nullDataIcon:'/images/icon/nullData.jpg',
    handler: '',
    roleId: '',
    initiator: '',
    name: '',
    code: '',
    date: '',
    dateIndex: '#',
    searchKeyword: '',
    /**
     * showResearch控制搜索按钮状态
     * 0 表示 进行搜索
     * 1 表示 点完放大镜后弹出搜索框
     * 2 表示 清空搜索条件
     */
    showResearch: 0,
    isSearchResult: false,
    showDeviceInfo: false,
    deviceInfo:{
      id: '',
      code: '',
      installTime: '',
      lastCheckTime: '',
      model: '',
      name: '',
      namePosition: '',
      position: '',
      producCapacity: '',
      produceDate: '',
      producer: '',
      producerPhone: '',
      ratedPower: '',
      typeCode: '',
      whichClick: -1
    },
  },

  // 搜索框的日期选择器 
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  // 点击搜索放大镜,显示自定义弹窗
  buildNewResearch: function(e){
    this.setData({
      showResearch: 1
    })
  },
  // 点击重置图标,返回搜索前的数据
  clearOldResearch: function(e){
    this.setData({
      isSearchResult: false,
      showResearch: 0
    })
    this.onShow();
  },
  // 点击返回按钮隐藏
  back: function(){
    this.setData({
      showResearch: 0,
      dateIndex: '#'
    })
  },
  // 获取input输入值
  wish_put: function(e){
    this.setData({
      searchKeyword: e.detail.value
    })
  },
  // 点击 "搜索" 按钮
  doResearch: function(){
    this.setData({
      isSearchResult: true,
    })
    // 刷新页面
    this.onShow()
  },

  // 点击记录的列表项跳转到任务详情页面
  toRrdetails(e){
    // console.log(typeof(this.data.roleId))
    let taskIndex = e.currentTarget.dataset.index
    // console.log(e.currentTarget.dataset.index)
    let id = this.data.taskList[taskIndex].id 
    let typeCode = this.data.taskList[taskIndex].type
    let deviceName = this.data.taskList[taskIndex].deviceName+''
    let deviceCode = this.data.taskList[taskIndex].deviceCode
    let code = this.data.code
    let whichClick = this.data.deviceInfo.whichClick
    // console.log(this.data.taskList[taskIndex])
    if(deviceName !== "null"){
      // console.log(typeof(deviceName))
        this.data.showDeviceInfo = true
    }else {
      this.data.showDeviceInfo = false
    }
    let showDeviceInfo = this.data.showDeviceInfo
    // console.log(showDeviceInfo)
    wx.navigateTo({
      //要跳转到的页面路径
      url: '/pages/rrdetails/rrdetails?id='+id+
      '&code='+code+
      '&typeCode='+typeCode+
      '&showDeviceInfo='+showDeviceInfo+
      '&deviceCode='+deviceCode+
      '&whichClick='+whichClick
    })
  },

  // '点击查看维修记录',跳转到该设备的维修记录页面,
  // 在rrdetails的onload中进入 deviceInfo.whichClick==0 的分支
  toRepairRecode(e){
    console.log(e)
    this.setData({
      // 设置deviceInfo.whichClick==0 是为了在reviceReport页面进入deviceInfo.whichClick!=10的分支
      // 此时显示该设备的所有维修记录列表 ,而不是单个设备的详细信息
      ['deviceInfo.whichClick']: 0,
    })
    // console.log(this.data.deviceInfo.whichClick)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // wx.stopPullDownRefresh() ;
    console.log(options)
    let persInfo = wx.getStorageSync('persInfo');
    let handler = persInfo.name
    let deviceCode = options.deviceCode
    let whichClick = options.whichClick;
    let initiator = persInfo.name
    let code = options.code
    let isSearchResult = this.data.isSearchResult
    let createDate = this.data.date
    let fuzzyWord = this.data.searchKeyword
    console.log(fuzzyWord)

    this.setData({
      code: code
    })

    // 如果deviceInfo.whichClick==10 则进入reviceReport.wxml页面的deviceInfo.whichClick==10的分支
    // 此时显示对应设备的所有信息
    if(whichClick == 10){
      this.setData({
        isNull: false,
      })

      // 查看某设备的所有信息
      request2({
        url: "/servlet/device/list",
        // method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          code: deviceCode,
        }
      })
      .then(result => {
        console.log(result.data.rows[0]);
        let row = result.data.rows[0];
        this.setData({
          ['deviceInfo.id']: row.id,
          ['deviceInfo.code']: row.code,
          ['deviceInfo.installTime']: row.installTime,
          ['deviceInfo.lastCheckTime']: row.lastCheckTime,
          ['deviceInfo.model']: row.model,
          ['deviceInfo.name']: row.name,
          ['deviceInfo.namePosition']: row.namePosition,
          ['deviceInfo.position']: row.position,
          ['deviceInfo.producCapacity']: row.producCapacity,
          ['deviceInfo.produceDate']: row.produceDate,
          ['deviceInfo.producer']: row.producer,
          ['deviceInfo.producerPhone']: row.producerPhone,
          ['deviceInfo.ratedPower']: row.ratedPower,
          ['deviceInfo.typeCode']: row.typeCode,
          ['deviceInfo.whichClick']: whichClick,
        })
      })
      .catch(err => {})

      // 查看该设备对应的维修记录
      request2({
        url: "/servlet/task/query",
        data: {
          page: 1,
          rows: 99,
          deviceCode: deviceCode
        }
      })
      .then(result => {
        this.setData({
          code: code, 
        })
        console.log(result) 
        console.log(this.data.isNull) 
        let rows = result.data.data.rows.length; 
        console.log(rows)
        if(rows == 0){ 
          this.setData({
            isNull: true
          })
        }else if(rows > 0){
          this.setData({
            isNull: false,
            taskList: result.data.data.rows,  
            id: result.data.data.rows,
            // 控制维修工人列表展示内容页面的数据
            name: persInfo.name,
            roleId: persInfo.roleId
          })
        }
        console.log(this.data.isNull) 
      })
      .catch(err => {}) 
    }else{ //如果没有进入查看设备详情页面,则点击不同功能模块,发送不同请求
      if(code == 1002){
        request2({
          url: "/servlet/task/query",
          // url: "/servlet/task/query4Check1",
          data: {
            page: 1,
            rows: 99,
            status: 1
          }
        })
        .then(result => {
          this.setData({
            code:code
          })
          console.log(result) 
          let rows = result.data.data.rows.length; 
          if(rows > 0){ 
            this.setData({
              isNull: false,
              taskList: result.data.data.rows,  
              id: result.data.data.rows,
              // 控制维修工人列表展示内容页面的数据
              name: persInfo.name,
              roleId: persInfo.roleId
            })
          }
        })
        .catch(err => {}) 
      } else if(code == 1003){
        request2({
          url: "/servlet/task/query?"+"page=1&rows=99&status=4",
          // url: "/servlet/task/query4Check1",
          data: {
    
          }
        })
        .then(result => {
          this.setData({
            code:code
          })
          console.log(result) 
          let rows = result.data.data.rows.length; 
          if(rows > 0){ 
            this.setData({
              isNull: false,
              taskList: result.data.data.rows,  
              id: result.data.data.rows,
              // 控制维修工人列表展示内容页面的数据
              name: persInfo.name,
              roleId: persInfo.roleId
            })
          }
        })
        .catch(err => {}) 
      } else if(code == 1007){
        request2({
          url: "/servlet/task/query?"+"page=1&rows=99&status=2",
          // url: "/servlet/task/query4Check1",
          data: {
    
          }
        })
        .then(result => {
          this.setData({
            code:code
          })
          console.log(result) 
          let rows = result.data.data.rows.length; 
          if(rows > 0){ 
            this.setData({
              isNull: false,
              taskList: result.data.data.rows,  
              id: result.data.data.rows,
              // 控制维修工人列表展示内容页面的数据
              name: persInfo.name,
              roleId: persInfo.roleId
            })
          }
        })
        .catch(err => {}) 
      } else if(code == 1015){
        // 如果在历史任务中进行搜索
        if(isSearchResult){
          request2({
            url: "/servlet/task/query",
            // url: "/servlet/task/query4Check1",
            data: {
              page: 1,
              rows: 99,
              status: 9,
              // createDate: createDate,
              fuzzyWord: fuzzyWord,
            }
          })
          .then(result => {
            this.setData({
              code:code,
              isNull: true,
            })
            console.log(result) 
            let rows = result.data.data.rows.length; 
            if(rows > 0){ 
              this.setData({
                isNull: false,
                taskList: result.data.data.rows,  
                id: result.data.data.rows,
                // 控制维修工人列表展示内容页面的数据
                name: persInfo.name,
                roleId: persInfo.roleId
              })
            }
            this.setData({
              showResearch: 2
            })
            wx.showToast({
              title: '搜索成功',
              icon: 'success',
              duration: 800
            })
          })
          .catch(err => {
            wx.showToast({
              title: '搜索失败',
              icon: 'error',
              duration: 800
            })
          }) 
        }else{
          request2({
            url: "/servlet/task/query",
            // url: "/servlet/task/query4Check1",
            data: {
              page: 1,
              rows: 99,
              status: 9
            }
          })
          .then(result => {
            this.setData({
              code:code
            })
            console.log(result) 
            let rows = result.data.data.rows.length; 
            if(rows > 0){ 
              this.setData({
                isNull: false,
                taskList: result.data.data.rows,  
                id: result.data.data.rows,
                // 控制维修工人列表展示内容页面的数据
                name: persInfo.name,
                roleId: persInfo.roleId
              })
            }
          })
          .catch(err => {}) 
        }

      } else if(code == 2010){
        request2({
          url: "/servlet/task/query",
          // url: "/servlet/task/query4Check1",
          data: {
            page: 1,
            rows: 99,
            status: 0,
            initiator: initiator
          }
        })
        .then(result => {
          this.setData({
            code:code
          })
          console.log(result) 
          let rows = result.data.data.rows.length; 
          if(rows > 0){ 
            this.setData({
              isNull: false,
              taskList: result.data.data.rows,  
              id: result.data.data.rows,
              // 控制维修工人列表展示内容页面的数据
              name: persInfo.name,
              roleId: persInfo.roleId
            })
          }
        })
        .catch(err => {}) 
      } else if(code == 2015){
        // 如果在历史任务中进行搜索
        if(isSearchResult){
          request2({
            url: "/servlet/task/query",
            // url: "/servlet/task/query4Check1",
            data: {
              page: 1,
              rows: 99,
              initiator: initiator,
              // createDate: createDate,
              fuzzyWord: fuzzyWord,
            }
          })
          .then(result => {
            this.setData({
              code: code,
              isNull: true,
            })
            console.log(result) 
            let rows = result.data.data.rows.length; 
            if(rows > 0){ 
              this.setData({
                isNull: false,
                taskList: result.data.data.rows,  
                id: result.data.data.rows,
                // 控制维修工人列表展示内容页面的数据
                name: persInfo.name,
                roleId: persInfo.roleId
              })
            }
            this.setData({
              showResearch: 2
            })
            wx.showToast({
              title: '搜索成功',
              icon: 'success',
              duration: 800
            })
          })
          .catch(err => {
            wx.showToast({
              title: '搜索失败',
              icon: 'error',
              duration: 800
            })
          }) 
        }else{
          request2({
            url: "/servlet/task/query",
            // url: "/servlet/task/query4Check1",
            data: {
              page: 1,
              rows: 99,
              initiator: initiator
            }
          })
          .then(result => {
            this.setData({
              code: code
            })
            console.log(result) 
            let rows = result.data.data.rows.length; 
            if(rows > 0){ 
              this.setData({
                isNull: false,
                taskList: result.data.data.rows,  
                id: result.data.data.rows,
                // 控制维修工人列表展示内容页面的数据
                name: persInfo.name,
                roleId: persInfo.roleId
              })
            }
          })
          .catch(err => {}) 
        }

      } else if(code == 3001){
        request2({
          url: "/servlet/task/query",
          // url: "/servlet/task/query4Check1",
          data: {
            page: 1,
            rows: 99,
            status: 2,
            handler: handler
          }
        })
        .then(result => {
          console.log(result)  
          let rows = result.data.data.rows.length
          this.setData({
            code: code
          })
          if(rows > 0){
            this.setData({
              isNull: false,
              taskList: result.data.data.rows,
              id: result.data.data.rows,
              name: persInfo.name,
              roleId: persInfo.roleId
            })
          }
          // console.log(this.data.taskList)
        })
        .catch(err => {}) 
      } else if(code == 3002){
        request2({
          url: "/servlet/task/query",
          // url: "/servlet/task/query4Check1",
          data: {
            page: 1,
            rows: 99,
            status: 3,
            handler: handler
          }
        })
        .then(result => {
          this.setData({
            code:code
          })
          console.log(result) 
          let rows = result.data.data.rows.length; 
          if(rows > 0){ 
            this.setData({
              isNull: false,
              taskList: result.data.data.rows,  
              id: result.data.data.rows,
              // 控制维修工人列表展示内容页面的数据
              name: persInfo.name,
              roleId: persInfo.roleId
            })
          }
        })
        .catch(err => {}) 
      } else if(code == 3003){
        request2({
          url: "/servlet/task/query",
          // url: "/servlet/task/query4Check1",
          data: {
            page: 1,
            rows: 99,
            status: 4,
            handler: handler
          }
        })
        .then(result => {
          this.setData({
            code:code
          })
          console.log(result) 
          let rows = result.data.data.rows.length; 
          if(rows > 0){ 
            this.setData({
              isNull: false,
              taskList: result.data.data.rows,  
              id: result.data.data.rows,
              // 控制维修工人列表展示内容页面的数据
              name: persInfo.name,
              roleId: persInfo.roleId
            })
          }
        })
        .catch(err => {}) 
      } else if(code == 3004){
        // 如果在历史任务中进行搜索
        if(isSearchResult){
          request2({
            url: "/servlet/task/query",
            data: {
              page: 1,
              rows: 99,
              status: 9,
              handler: handler,
              // createDate: createDate,
              fuzzyWord: fuzzyWord,
            }
          })
          .then(result => {
            this.setData({
              code:code,
              isNull: true,
            })
            console.log(result) 
            let rows = result.data.data.rows.length; 
            // console.log(rows) 
            if(rows > 0){ 
              this.setData({
                isNull: false,
                taskList: result.data.data.rows,  
                id: result.data.data.rows,
                // 控制维修工人列表展示内容页面的数据
                name: persInfo.name,
                roleId: persInfo.roleId
              })
            }
            this.setData({
              showResearch: 2
            })
            wx.showToast({
              title: '搜索成功',
              icon: 'success',
              duration: 800
            })
          })
          .catch(err => {
            wx.showToast({
              title: '搜索失败',
              icon: 'error',
              duration: 800
            })
          }) 
        }else{
          request2({
            url: "/servlet/task/query",
            // url: "/servlet/task/query4Check1",
            data: {
              page: 1,
              rows: 99,
              status: 9,
              handler: handler
            }
          })
          .then(result => {
            this.setData({
              code:code
            })
            console.log(result) 
            let rows = result.data.data.rows.length; 
            if(rows > 0){ 
              this.setData({
                isNull: false,
                taskList: result.data.data.rows,  
                id: result.data.data.rows,
                // 控制维修工人列表展示内容页面的数据
                name: persInfo.name,
                roleId: persInfo.roleId
              })
            }
          })
          .catch(err => {}) 
        }
      } 
    }




  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 调用该方法会刷新当前页面
    const pages = getCurrentPages()
    const perpage = pages[pages.length - 1]
    perpage.onLoad(this.options)  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.onShow(); 
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})